package com.lenovo.smarttraffic.ui.activity.stage4;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.gson.Gson;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.BusLineInfoBean;
import com.lenovo.smarttraffic.bean.UserBean;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.util.HttpUtils;
import com.lenovo.smarttraffic.util.ToastUtils;
import com.lenovo.smarttraffic.util.Utils;

import java.util.ArrayList;
import java.util.List;

public class BusOrderActivity extends BaseActivity {

    public static List<BusLineInfoBean.ROWSDETAILBean> beanList = CustomBusActivity.beanList;
    private BusLineInfoBean.ROWSDETAILBean bean;

    private UserBean.ROWS_DETAIL userBean;

    public static List<String> dayList = new ArrayList<>();

    private LinearLayout info_parent;
    private ImageView map_img;
    private LinearLayout calendar_parent;
    private TextView date_tv;
    private CalendarView calendarView;
    private TextView info_tv;
    private Button next_btn;

    private LinearLayout select_parent;
    private TextView road_tv, user_info_tv;
    private Spinner up_sp, down_sp;
    private Button next_btn2;

    private LinearLayout check_parent;
    private TextView road_check_tv, info_check_tv;
    private Button check_btn;

    private Handler handler = new Handler();

    private boolean can_set = true;

    @Override
    protected int getLayout() {
        return R.layout.activity_bus_order;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolBar(this, "定制班车");

        bean = beanList.get(getIntent().getIntExtra("pos", 0));

        info_parent = findViewById(R.id.info_parent);
        map_img = findViewById(R.id.map_img);
        calendar_parent = findViewById(R.id.calendar_parent);
        date_tv = findViewById(R.id.date_tv);
        calendarView = findViewById(R.id.calendar_view);
        info_tv = findViewById(R.id.info_tv);
        next_btn = findViewById(R.id.next_btn);

        select_parent = findViewById(R.id.select_parent);
        road_tv = findViewById(R.id.road_tv);
        user_info_tv = findViewById(R.id.user_info_tv);
        up_sp = findViewById(R.id.up_sp);
        down_sp = findViewById(R.id.down_sp);
        next_btn2 = findViewById(R.id.next_btn2);

        check_parent = findViewById(R.id.check_parent);
        road_check_tv = findViewById(R.id.road_check_tv);
        info_check_tv = findViewById(R.id.info_check_tv);
        check_btn = findViewById(R.id.check_btn);

        initView1();
    }

    @SuppressLint("SetTextI18n")
    private void initView1() {
        info_tv.setText(bean.getStart_place() + "————" + bean.getEnd_place() + "\n\n\n" +
                "票价：￥" + bean.getPrice() + "\n\n" +
                "里程：" + bean.getDistance() + "km");

        date_tv.setText(calendarView.getCurYear() + "年" + (calendarView.getCurMonth() < 10 ? "0" : "") + calendarView.getCurMonth() + "月 - " +
                calendarView.getCurYear() + "年" + (calendarView.getCurMonth() < 10 ? "0" : "") + (calendarView.getCurMonth() + 1) + "月");

        calendarView.setOnCalendarMultiSelectListener(new CalendarView.OnCalendarMultiSelectListener() {
            @Override
            public void onCalendarMultiSelectOutOfRange(Calendar calendar) {
            }

            @Override
            public void onMultiSelectOutOfSize(Calendar calendar, int maxSize) {
            }

            @Override
            public void onCalendarMultiSelect(Calendar calendar, int curSize, int maxSize) {
                handler.postDelayed(() -> info_tv.setText("乘车日期：\n" + dayList.toString().replace("[", "").replace("]", "")), 100);
            }
        });

        next_btn.setOnClickListener(v -> {
            if (map_img.getVisibility() == View.VISIBLE) {
                map_img.setVisibility(View.GONE);
                calendar_parent.setVisibility(View.VISIBLE);
                info_tv.setText("请选择乘车日期");
            } else if (dayList.size() < 1) {
                ToastUtils.showToast(BusOrderActivity.this, "至少选择一天");
            } else {
                info_parent.setVisibility(View.GONE);
                select_parent.setVisibility(View.VISIBLE);

                initView2();
            }
        });
    }

    @SuppressLint("SetTextI18n")
    private void initView2() {
        road_tv.setText(bean.getStart_place() + "————" + bean.getEnd_place());

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, bean.getBus_stop().substring(0, bean.getBus_stop().length() - 1).split(","));
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        up_sp.setAdapter(adapter);
        down_sp.setAdapter(adapter);

        HttpUtils.post("get_all_user_info", new HttpUtils().add("UserName", MainActivity.userName), response -> {
            List<UserBean.ROWS_DETAIL> rows_detail = new Gson().fromJson(response.toString(), UserBean.class).getROWS_DETAIL();
            for (int i = 0; i < rows_detail.size(); i++) {//遍历获取当前用户位置
                if ((userBean = rows_detail.get(i)).getUsername().equals(MainActivity.userName)) {
                    break;
                }
            }
            user_info_tv.setText("乘客姓名：" + userBean.getPname() + "\n手机号码：" + userBean.getPtel());
        });

        next_btn2.setOnClickListener(v -> {
            if (up_sp.getSelectedItemPosition() == down_sp.getSelectedItemPosition()) {
                ToastUtils.showToast(BusOrderActivity.this, "上车地点不能与下车地点一致");
            } else {
                select_parent.setVisibility(View.GONE);
                check_parent.setVisibility(View.VISIBLE);

                initView3();
            }
        });
    }

    @SuppressLint("SetTextI18n")
    private void initView3() {
        initToolBar(this, "确定订单");

        road_check_tv.setText("乘车路线：" + bean.getStart_place() + "————" + bean.getEnd_place());
        info_check_tv.setText("乘客姓名：" + userBean.getPname() + "\n手机号码：" + userBean.getPtel() +
                "\n上车地点：" + up_sp.getSelectedItem() + "\n下车地点：" + down_sp.getSelectedItem() +
                "\n乘车日期：" + dayList.toString().replace("[", "").replace("]", ""));

        check_btn.setOnClickListener(v -> {
            if (can_set) {
                can_set = false;

                ProgressDialog progressDialog = new ProgressDialog(this);
                progressDialog.setTitle("请稍后");
                progressDialog.setMessage("发送请求中");
                progressDialog.setCancelable(false);
                progressDialog.show();

                HttpUtils.post("set_busline_order", new HttpUtils().add("UserName", MainActivity.userName)
                                .add("bus_id", bean.getId())
                                .add("user", userBean.getPname())
                                .add("phone_number", userBean.getPtel())
                                .add("busstop_start", (String) up_sp.getSelectedItem())
                                .add("busstop_end", (String) down_sp.getSelectedItem())
                                .add("busstop_date", dayList.toString().replace("[", "").replace("]", "")),
                        response -> {
                            if (Utils.getResult(response)) {
                                ToastUtils.showToast(BusOrderActivity.this, "提交成功");
                                finish();
                            } else {
                                ToastUtils.showToast(BusOrderActivity.this, "提交失败");
                            }
                            can_set = true;
                            progressDialog.dismiss();
                        });
            }
        });
    }

}
